package com.shardingsphere.shardingsphere.myShardingAlgorithm;

import groovy.util.logging.Slf4j;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Collection;

@Slf4j
public class ProjectAlgorithm implements PreciseShardingAlgorithm<String> {

	@Override
	public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
		System.out.println("preciseShardingValue"+preciseShardingValue);

		String createTime1=preciseShardingValue.getValue();
		//Timestamp createTime=preciseShardingValue.getValue();
		LocalDateTime date= LocalDateTime.parse(createTime1, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		ZoneId zone = ZoneId.systemDefault();
		Timestamp createTime=new Timestamp(date.atZone(zone).toInstant().toEpochMilli());
		int year=LocalDateTime.ofInstant(createTime.toInstant(), ZoneOffset.ofHours(0)).getYear();
		String table="project_";

		String result=table+(year%2);
		System.out.println("访问表"+result);
		return result;

	}


}
